package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import com.guodu.dao.DeviceInformationDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.DeviceInformation;
import com.guodu.entity.DeviceInformationStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.DeviceInformationService;
import com.guodu.vo.DeviceInformationQueryVo;
import com.guodu.vo.DeviceInformationVo;

@Service("deviceInformationService")
public class DeviceInformationServiceImpl implements DeviceInformationService{
	@Resource
	private DeviceInformationDao deviceInformationDao;
	@Resource
	private UnitsDao unitsDao;
	@Resource
	private UserInfoDao userInfoDao;

	
	public List<DeviceInformationVo> findByVo(
			DeviceInformationQueryVo deviceInformationQueryVo, Units units,
			int curPage, int pageSize) {
		List<DeviceInformationVo> deviceInformationVoList = new ArrayList<DeviceInformationVo>();
		List<DeviceInformation> deviceInformationList = deviceInformationDao.findByVo(deviceInformationQueryVo, units, curPage, pageSize);
		for(int i=0;i<deviceInformationList.size();i++){
			DeviceInformationVo deviceInformationVo = new DeviceInformationVo();
			deviceInformationVo.setDeviceInformation(deviceInformationList.get(i));			
			UserInfo userInfo = userInfoDao.findById(deviceInformationList.get(i).getCreatePeople());
//			Units unitsInfo = unitsDao.findById(Long.valueOf(deviceInformationList.get(i).getUnitsId()));
//			if(unitsInfo.getUnitsLevel()==2){
//			deviceInformationVo.setUnitsName(unitsInfo.getUnitsName());
//			deviceInformationVo.setIndustry(unitsInfo.getIndustry());
//			deviceInformationVo.setUnitsId(unitsInfo.getUnitsId());
//			Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
//			deviceInformationVo.setFunitsName(funitsInfo.getUnitsName());	
//			deviceInformationVo.setFunitsId(unitsInfo.getFunitsId());
//			}else{
//				deviceInformationVo.setFunitsName(unitsInfo.getUnitsName());
//				deviceInformationVo.setFunitsId(unitsInfo.getUnitsId());
//			}
//			Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
//			deviceInformationVo.setUnitsLevel(createUnits.getUnitsLevel());
			
			if(deviceInformationList.get(i).getUnitsId() != null){
				Units unitsInfo = unitsDao.findById(Long.valueOf(deviceInformationList.get(i).getUnitsId()));
				if(unitsInfo.getUnitsLevel()==2L){
					deviceInformationVo.setUnitsName(unitsInfo.getUnitsName());
					deviceInformationVo.setIndustry(unitsInfo.getIndustry());
					deviceInformationVo.setUnitsId(unitsInfo.getUnitsId());
					Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
					deviceInformationVo.setFunitsName(funitsInfo.getUnitsName());	
					deviceInformationVo.setFunitsId(unitsInfo.getFunitsId());
				}else{
					deviceInformationVo.setFunitsName(unitsInfo.getUnitsName());
					deviceInformationVo.setFunitsId(unitsInfo.getUnitsId());
				}
			}
			if(userInfo.getUnitsId() != null){
				Units createUnits =  unitsDao.findById(userInfo.getUnitsId());
				deviceInformationVo.setUnitsLevel(createUnits.getUnitsLevel());				
			}else{
				deviceInformationVo.setJt("jt");
			}				
			deviceInformationVo.setUserName(userInfo.getUserName());
			deviceInformationVoList.add(deviceInformationVo);		
		}	
		return deviceInformationVoList;
	}

	
	public int findSizeByVo(DeviceInformationQueryVo deviceInformationQueryVo,
			Units units) {
		
		return deviceInformationDao.findSizeByVo(deviceInformationQueryVo, units);
	}

	public void save(DeviceInformation deviceInformation) {
		deviceInformationDao.save(deviceInformation);
		
	}

	public void examine(String ids, String reason, String beforeStatus,
			Long afterStatus, UserInfo userInfo) {
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<DeviceInformationStatus> deviceInformationStatuslist = new ArrayList<DeviceInformationStatus>();
		for(int i=0;i<idses.length;i++){
			DeviceInformationStatus deviceInformationStatus = new DeviceInformationStatus();
			deviceInformationStatus.setAfterStatus(afterStatus);
			deviceInformationStatus.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			deviceInformationStatus.setCreatePeople(userInfo.getUserId());
			deviceInformationStatus.setDeviceId(idses[i]);
			deviceInformationStatus.setReason(reason);
			deviceInformationStatus.setCreateTime(Calendar.getInstance().getTime());
			deviceInformationStatus.setStatusId(i+"");
			deviceInformationStatuslist.add(deviceInformationStatus);			
		}
		deviceInformationDao.changeStatus(deviceInformationStatuslist);
		deviceInformationDao.examine(ids, afterStatus);
	}

	public void delByIds(String[] ids) {
		deviceInformationDao.delByIds(ids);
		
	}

	public DeviceInformation findById(String id) {
		
		return deviceInformationDao.findById(id);
	}


	
	public void update(DeviceInformation deviceInformation,Long userId) {		
		if(deviceInformation.getStatus() == 4L || deviceInformation.getStatus() == 2L){
			List<DeviceInformationStatus> deviceInformationStatuslist = new ArrayList<DeviceInformationStatus>();
			DeviceInformationStatus deviceInformationStatus = new DeviceInformationStatus();
			deviceInformationStatus.setAfterStatus(0L);
			deviceInformationStatus.setBeforeStatus(deviceInformation.getStatus());
			deviceInformationStatus.setCreatePeople(userId);
			deviceInformationStatus.setDeviceId(deviceInformation.getDeviceId());
			deviceInformationStatus.setReason("");
			deviceInformationStatus.setCreateTime(Calendar.getInstance().getTime());
			deviceInformationStatus.setStatusId("1");
			deviceInformationStatuslist.add(deviceInformationStatus);
			deviceInformationDao.changeStatus(deviceInformationStatuslist);
		}
		deviceInformation.setStatus(0L);
//		Units units = (Units)ServletActionContext.getContext().getSession().get("unitsbean");
//		if(units == null){
//			deviceInformation.setStatus(3L);
//		}
		deviceInformationDao.update(deviceInformation);
		
	}


	
	public List<DeviceInformationStatus> findStatus(String id) {
		
		return deviceInformationDao.findStatus(id);
	}

	public void deleteById(String id) {
		deviceInformationDao.deleteById(id);
		
	}


	public List<DeviceInformation> downLoad(
			DeviceInformationQueryVo deviceInformationQueryVo,Units units) {
		
		return deviceInformationDao.downLoad(deviceInformationQueryVo,units);
	}


	
	public void examinAll(DeviceInformationQueryVo deviceInformationQueryVo,
			Units units) {
		deviceInformationDao.examinAll(deviceInformationQueryVo,units);
		
	}
	
	
}
